package scau.erps.view.decision.sand;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import scau.erps.app.Erps;
import scau.erps.entity.Line;
import scau.erps.entity.Product;
import scau.erps.service.FactoryService;
import scau.erps.service.LineService;
import scau.erps.service.ProductService;
import scau.erps.service.impl.FactoryServiceImpl;
import scau.erps.service.impl.LineServiceImpl;
import scau.erps.service.impl.ProductServiceImpl;

/**
 * 产房盘面模拟面板
 * @author DCJ
 *
 */
public class FactoryPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private FactoryService factoryService=Erps.getIocInstance().get(FactoryServiceImpl.class);
	private LineService lineService=Erps.getIocInstance().get(LineServiceImpl.class);
	private ProductService productService=Erps.getIocInstance().get(ProductServiceImpl.class);
	private JPanel factoryPanel;
	private LinePanel[] linePanel=new LinePanel[8];
	private JPanel bigFactory,middleFactory;
	public FactoryPanel(){
		factoryPanel = new JPanel(new GridLayout(1,2));	
		bigFactory = new JPanel();
		bigFactory.setBorder(BorderFactory.createTitledBorder("大厂房"));
		bigFactory.setLayout(new GridLayout(1,4));			
	
		middleFactory = new JPanel();
		middleFactory.setBorder(BorderFactory.createTitledBorder("中/小厂房"));		
		middleFactory.setLayout(new GridLayout(1,3));
	    setLineData();
		factoryPanel.add(bigFactory);
		factoryPanel.add(middleFactory);		
		this.add(factoryPanel);
	}
	public void setDimension(Dimension d){
		this.factoryPanel.setPreferredSize(d);
	}
	private void setLineData(){              //获取该小组生产线数据以绘制LinePanel
		bigFactory.removeAll();
		middleFactory.removeAll();
		List<Line> lineList=lineService.getLineByTeamId(Erps.getCurrentTeam().getId());
		for (int i=0;i<=7;i++){
			Product product=null;
			Line line=lineList.get(i);
			if (line.getStatus().equals("空置")) {
				linePanel[line.getLineNo()-1]=new LinePanel(null, null, 0,product);
				continue;
			}
			else {
				if (line.getStatus().equals("生产")){
					product=productService.getProductByLineNoAndStatus(Erps.getSand().getTeam_Id(),line.getLineNo(),"在制品").get(0);
				}
				linePanel[line.getLineNo()-1]=new LinePanel(line.getProduct(), line.getType(), line.getValue(),product);
			}
		}
		bigFactory.add(linePanel[0]);
		bigFactory.add(linePanel[1]);
		bigFactory.add(linePanel[2]);
		bigFactory.add(linePanel[3]);	
		middleFactory.add(linePanel[4]);
		middleFactory.add(linePanel[5]);
		middleFactory.add(linePanel[6]);
		middleFactory.add(linePanel[7]);	
		
	}

	public void refreshLineData(){
		setLineData();
	}

}
